﻿#include "piece.h"

const QSize Piece::s_QuadLength(25, 25);

Piece::Piece(){}

Piece::~Piece(){}

void Piece::moveRight()
{
    for (int i=0; i < m_Quadrados.size(); i++)
        m_Quadrados[i].moveRight(m_Quadrados[i].right() + s_QuadLength.width());
}

void Piece::moveLeft()
{
    for (int i=0; i < m_Quadrados.size(); i++)
        m_Quadrados[i].moveLeft(m_Quadrados[i].left() - s_QuadLength.width());
}

void Piece::moveDown()
{
    for (int i=0; i < m_Quadrados.size(); i++)
        m_Quadrados[i].moveBottom(m_Quadrados[i].bottom() + s_QuadLength.height());
}

void Piece::centerPieceWidth(int w)
{
    int xMin = (int)HUGE_VAL, xMax = (int)-HUGE_VAL;

    for (int i=0; i < m_Quadrados.size(); i++){
        if(m_Quadrados[i].left()   < xMin) xMin = m_Quadrados[i].left();
        if(m_Quadrados[i].right()  > xMax) xMax = m_Quadrados[i].right();
    }

    for (int i=0; i < m_Quadrados.size(); i++)
        m_Quadrados[i].moveLeft(m_Quadrados[i].left() +
                              ((w - (xMax - xMin))/2));
}

void Piece::centerPieceHeight(int h)
{
    int yMin = (int)HUGE_VAL, yMax = (int)-HUGE_VAL;

    for (int i=0; i < m_Quadrados.size(); i++){
        if(m_Quadrados[i].top()    < yMin) yMin = m_Quadrados[i].top();
        if(m_Quadrados[i].bottom() > yMax) yMax = m_Quadrados[i].bottom();
    }

    for (int i=0; i < m_Quadrados.size(); i++)
        m_Quadrados[i].moveTop(m_Quadrados[i].top() +
                               ((h - (yMax - yMin))/2));
}

void Piece::setQuadrados(QList<QRect> quads){
    if(m_TriedRot){
        m_Rot = !m_Rot;
        m_TriedRot = false;
    }
    m_Quadrados = quads;
}
